package dao

import (
	"fmt"
	"github.com/jmoiron/sqlx"
	w "yun_image/config/dataBase/weibo-pachong"
	"yun_image/entity"
	"yun_image/util"
)

type HotDao struct {
}

var weiboDB *sqlx.DB

func initWeiboDB() {
	dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", w.Username, w.Password, w.Host, w.Port, w.Dbname)
	db, err := sqlx.Open("mysql", dataSourceName)
	if err != nil {
		fmt.Println(util.Red("weiboDb 数据库连接失败"))
	}
	weiboDB = db
}

func (h *HotDao) GetHotsByWord(hots *[]entity.Hot, word string) error {
	sql := "select hotword, type, tag, times, first_timestick, last_timestick, top_order, bottom_order from hot_info where hotword like concat('%', ?, '%') order by first_timestick limit ? "
	err := weiboDB.Select(hots, sql, word, w.LimitNumber)
	return err
}

func (h *HotDao) GetHotsByTime(hots *[]entity.Hot, startTime int64, endTime int64) error {
	sql := "select hotword, type, tag, times, first_timestick, last_timestick, top_order, bottom_order from hot_info where first_timestick < ? and last_timestick > ? order by first_timestick limit ?"
	err := weiboDB.Select(hots, sql, endTime, startTime, w.LimitNumber)
	return err
}
func (h *HotDao) GetBaiduByTime(hots *[]entity.BaiduHot, startTime int64, endTime int64) error {
	result := DB.Where("first_time < ? and last_time > ? order by first_time limit ?",
		endTime, startTime, w.LimitNumber).Find(hots)
	return result.Error
}
func (h *HotDao) GetBaiduByWord(hots *[]entity.BaiduHot, word string) error {
	result := DB.Where("word like concat('%', ?, '%') order by first_time limit ?", word, w.LimitNumber).Find(hots)
	return result.Error
}

func (h *HotDao) GetFengHuangByTime(hots *[]entity.FengHuangHot, startTime int64, endTime int64) error {
	result := DB.Where("first_time < ? and last_time > ? order by first_time limit ?",
		endTime, startTime, w.LimitNumber).Find(hots)
	return result.Error
}
func (h *HotDao) GetFengHuangByWord(hots *[]entity.FengHuangHot, word string) error {
	result := DB.Where("word like concat('%', ?, '%') order by first_time limit ?", word, w.LimitNumber).Find(hots)
	return result.Error
}

func (h *HotDao) GetBiliByTime(hots *[]entity.BiliHot, startTime int64, endTime int64) error {
	result := DB.Where("first_time < ? and last_time > ? order by first_time limit ?",
		endTime, startTime, w.LimitNumber).Find(hots)
	return result.Error
}
func (h *HotDao) GetBiliByWord(hots *[]entity.BiliHot, word string) error {
	result := DB.Where("word like concat('%', ?, '%') order by first_time limit ?", word, w.LimitNumber).Find(hots)
	return result.Error
}
